#! /usr/bin/env python

VERSION='0.1'
APPNAME='tools'

srcdir = '.'
blddir = 'build'

import sys, re
import waf_dynamo, waf_ddf
from BuildUtility import create_build_utility
import waflib.Options
import TestContext
from build_constants import TargetOS

test_context = None

def init(ctx):
    global test_context
    if 'build' in waflib.Options.commands:
        test_context = TestContext.create_test_context()

def options(opt):
    opt.load('waf_dynamo')

def configure(conf):
    conf.load('waf_dynamo')

    target_os = conf.env['TARGET_OS']
    try:
        build_util = create_build_utility(conf.env)
    except BuildUtilityException as ex:
        conf.fatal(ex.msg)

    waf_ddf.configure(conf)

    conf.recurse('src')

    platform = conf.env['PLATFORM']

    glfw_library = 'dmglfw'

    if waf_dynamo.platform_glfw_version(conf.env.PLATFORM) == 3:
        glfw_library = 'glfw3'

    if target_os == TargetOS.MACOS:
        conf.env.append_value('LINKFLAGS', ['-framework', 'Cocoa', '-framework', 'OpenGL', '-framework', 'IOKit', '-framework', 'Carbon', '-framework', 'CoreVideo', '-framework', 'QuartzCore'])
    elif platform == 'x86_64-ios':
        pass
    elif target_os == TargetOS.WINDOWS:
        conf.env.append_value('LINKFLAGS', ['opengl32.lib', 'user32.lib', 'shell32.lib', 'xinput9_1_0.lib', 'dinput8.lib', 'dxguid.lib'])
    elif target_os == TargetOS.ANDROID:
        conf.env.append_value('LINKFLAGS', ['-lEGL', '-lGLESv1_CM', '-lGLESv2', '-landroid'])

    conf.env.append_value('INCLUDES', "default/src")
    conf.env['STLIB_DLIB'] = ['dlib', 'mbedtls', 'zip']
    conf.env['STLIB_DDF'] = 'ddf'
    conf.env['STLIB_TEXC'] = ['texc', 'basis_encoder']
    conf.env['STLIB_DMGLFW'] = glfw_library
    conf.env['STLIB_GRAPHICS'] = 'graphics'
    conf.env['STLIB_GRAPHICS_WEBGPU'] = ['graphics_webgpu']
    conf.env['STLIB_GRAPHICS_VULKAN'] = ['graphics_vulkan']
    conf.env['STLIB_GRAPHICS_PS4'] = ['graphics_ps4']
    if target_os == TargetOS.MACOS:
        conf.env['STLIB_GRAPHICS_VULKAN'] += ['MoltenVK']

    conf.env.append_unique('DEFINES', 'DLIB_LOG_DOMAIN="TOOLS"')
    conf.env.append_unique('DEFINES', 'DEFOLD_SHA1="%s"' % build_util.git_sha1())

def build(bld):
    global test_context
    bld.recurse('scripts')
    bld.recurse('src')
    TestContext.initialize_test_context(test_context, bld)

def shutdown(self):
    waf_dynamo.run_tests(test_context, valgrind = True)
